import { timeFormat } from '../../utils/util'
const list = [
    {
        logo: '../../img/default.png',
        store_name: '众利网络科技薯条部',
        status: '0',
        cur_ctime: '1506339900',         // 当前时间
        over_ctime: '1506340500',       // 最后支付时间
        order_ctime: '1506336900',       // 订单时间
        price: '18.3',
        goods: [
            { name: '大薯条' },
            { name: '中薯条' },
            { name: '小薯条' }
        ]
    },
    {
        logo: '../../img/default.png',
        store_name: '众利网络科技可乐部',
        status: '1',
        cur_ctime: '1506327600',         // 当前时间
        over_ctime: '1506330000',       // 最后支付时间
        order_ctime: '1506323442',       // 订单时间
        price: '18.4',
        goods: [
            { name: '大可乐' }
        ]
    },
    {
        logo: '../../img/default.png',
        store_name: '众利网络科技苹果部',
        status: '2',
        cur_ctime: '1506327600',         // 当前时间
        over_ctime: '1506330000',       // 最后支付时间
        order_ctime: '1506323442',       // 订单时间
        price: '18.5',
        goods: [
            { name: '大苹果' },
            { name: '中苹果' }
        ]
    },
    {
        logo: '../../img/default.png',
        store_name: '众利网络科技梨子部',
        status: '3',
        cur_ctime: '1506327600',         // 当前时间
        over_ctime: '1506330000',       // 最后支付时间
        order_ctime: '1506323442',       // 订单时间
        price: '18.1',
        goods: [
            { name: '大梨子' },
            { name: '中梨子' },
            { name: '小梨子' }
        ]
    },
    {
        logo: '../../img/default.png',
        store_name: '众利网络科技汉堡部',
        status: '4',
        cur_ctime: '1506327600',         // 当前时间
        over_ctime: '1506330000',       // 最后支付时间
        order_ctime: '1506323442',       // 订单时间
        price: '18.2',
        goods: [
            { name: '大汉堡' },
            { name: '中汉堡' },
            { name: '小汉堡' },
            { name: '大梨子' },
        ]
    }
];

Page({
    data: {
        // 0 待支付 1待配送 2配送中 3配送完成 4订单取消
        list: list
    },
    // 格式化时间
    format_date: function (time) {
        let date = new Date(time * 1000);
        let m, s;
        m = date.getMinutes();
        s = date.getSeconds();
        m < 10 ? m = '0' + date.getMinutes() : null;
        s < 10 ? s = '0' + date.getSeconds() : null;

        let d1 = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
        let d2 = date.getHours() + ":" + m + ":" + s;

        let _date = d1 + " " + d2;
        return _date;
    },
    // 格式化时间2
    format_time: function (time) {
        let m, s, obj = {};
        m = parseInt(time / 60);
        s = time - (m * 60);
        m < 10 ? m = '0' + m : null;
        s < 10 ? s = '0' + s : null;
        obj = { m: m, s: s };
        return obj;
    },
    // =====================处理下单时间 ============
    // 下单时间计算 gt_oneDay 大于一天吗?
    Orders_time_progress: function (time, order_ctime, gt_oneDay = false) {
        if (gt_oneDay) {
            let { _date } = this.format_date(order_ctime);
        } else {
            let h, m, s;
            h = parseInt(time / 3600);
            m = parseInt((time - (h * 3600)) / 60);
            s = parseInt(time - (h * 3600) - (m * 60));

            if (h == 0) {
                if (m == 0) {
                    return s + '秒前';
                } else {
                    if (s == 0) {
                        return m + '分钟前';
                    } else if (s > 0 && s < 10) {
                        return m + '分钟' + '0' + s + '秒前';
                    } else {
                        return m + '分钟' + s + '秒前';
                    }
                }
            } else {
                // 有小时了 m>0&&m<10 的话前面加0
                if (m == 0) {
                    if (s == 0) {
                        return h + '小时前';
                    } else {
                        if (s < 10 && s > 0) {
                            return h + '小时' + '00分钟' + '0' + s + '秒前';
                        }
                    }
                } else if (m > 0 && m < 10) {
                    if (s == 0) {
                        return h + '小时' + '0' + m + '分钟前';
                    } else if (s < 10) {
                        return h + '小时' + '0' + m + '分钟' + '0' + s + '秒前';
                    } else {
                        return h + '小时' + '0' + m + '分钟' + s + '秒前';
                    }
                } else {
                    if (s == 0) {
                        return h + '小时' + m + '分钟';
                    } else if (s > 0 && s < 10) {
                        return h + '小时' + m + '分钟' + '0' + s + '秒';
                    } else {
                        return h + '小时' + m + '分钟' + s + '秒';
                    }
                }
            }
        }
    },
    // 下单时间显示
    Orders_time: function (list) {
        let diff_order;
        list.map((item, idx) => {
            // 下单时间 差 如果超过一天 86400秒
            let timestamp_order;
            diff_order = parseInt(item.cur_ctime) - parseInt(item.order_ctime);
            if (diff_order >= 86400) {
                timestamp_order = this.Orders_time_progress(diff_order, parseInt(item.order_ctime), true)
            } else {
                timestamp_order = this.Orders_time_progress(diff_order);
            };

            item.timestamp_order = timestamp_order;
            // 待支付倒计时预处理 remainTime : 支付剩余时间
            let remainTime, payTime;
            remainTime = parseInt(item.over_ctime) - parseInt(item.cur_ctime);
            payTime = this.format_time(remainTime)

            item.payTime = payTime;
            item.remainTime = remainTime;

            // 订单状态预处理
            let status_txt;
            item.status == "0" ?
                status_txt = '待支付'
                : item.status == "1" ?
                    status_txt = '待配送'
                    : item.status == "2" ?
                        status_txt = '配送中'
                        : item.status == "3" ?
                            status_txt = '配送完成'
                            : item.status == "3" ?
                                status_txt = '订单取消'
                                : null;
            item.status_txt = status_txt;

        });
        this.setData({ list: list });
    },
    // =====================处理待支付订单倒计时 ============
    countDown: function () {
        const that = this;
        let timer = setInterval(_ => {
            list.map((item) => {
                if (item.status == '0') {
                    item.remainTime -= 1;
                    let payTime = that.format_time(item.remainTime);
                    item.payTime = that.format_time(item.remainTime);
                    if (payTime.m == '09' && payTime.s == '50') {
                        item.status = 3;
                        item.status_txt = '订单取消'
                        clearInterval(timer);
                    }
                };
            });
            that.setData({
                list: list
            })
        }, 1000);
    },

    onLoad: function (options) {
        // 处理订单状态 时间戳
        const that = this;
        // 下单时间的时间戳显示
        this.Orders_time(list);
        this.countDown(list);

    },
    onPullDownRefresh: function () {
        console.log(15)
        const that = this;
        const { list } = this.data;
        let list2 = {
            logo: '../../img/default.png',
            store_name: 'hello',
            status: '4',
            cur_ctime: '1506327600',         // 当前时间
            over_ctime: '1506330000',       // 最后支付时间
            order_ctime: '1506323442',       // 订单时间
            price: '18.2',
            goods: [
                { name: '大汉堡' },
                { name: '中汉堡' },
                { name: '小汉堡' },
                { name: '大梨子' },
            ]
        };

        setTimeout(_ => {
            list.unshift(list2);
            that.setData({
                list: list
            });
            wx.stopPullDownRefresh();
        }, 1000)

    },
    onReachBottom: function () {

    },
    onShareAppMessage: function () {

    }
})